

import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/material.dart';
import 'package:flustars/flustars.dart';
import 'package:get/get.dart';
import 'package:moonplay/common/app_common.dart';
import 'package:moonplay/pages/common_page/common_text_page.dart';
import 'package:moonplay/provide/theme_provider.dart';
import 'package:provide/provide.dart';

class CmUtils {
  /**
   * aluo44  2020-06-12
   * openDialog()
   * 封装弹出dialog, 参数详解
   * title: 标题
   * showTitle: 是否显示标题, 当为false时整个widget都不显示
   * subTitle: 内容文字
   * centerChild: 自定义内容部分, 如果有自定义内容, 则内容文字不显示
   * backgroundColor: 背景颜色
   * borderRadius: 圆角大小
   * marginHorizontal: 距离左右二边的距离,默认为105
   * type: ==1时只有一个按钮  ==2时有二个按钮
   * interceptBack 是否拦截安卓返回键
   * isBottomBtnWidget: 是否显示底部buttonWidget, 默认显示
   * cancelText: 取消文字
   * confirmText: 确定文字
   * cancelColor: 取消文字颜色
   * confirmColor: 确定文字颜色
   * cancelFun: 取消方法回调
   * confirmFun: 确定方法回调
   */
  static bool openDialog({BuildContext context, String title, bool interceptBack = false, bool showTitle = true, String subTitle = "", Widget centerChild, 
  Color backgroundColor, BorderRadiusGeometry borderRadius, int type = 1, double marginHorizontal = 105, bool isBottomBtnWidget = true,
  String cancelText, String confirmText, Color cancelColor, Color confirmColor, Function cancelFun, Function confirmFun}) {
    Get.dialog(
      Material(
        type: MaterialType.transparency,
        child: Scaffold(
          backgroundColor: Colors.transparent,
          body: Provide<ThemeProvider>(
            builder: (context, child, theme) {
              return WillPopScope(
                onWillPop: (){
                  if(!interceptBack) {
                    Get.back();
                  }
                },
                child: Center(
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: <Widget>[
                      Container(
                        margin: EdgeInsets.symmetric(horizontal: ScreenUtil().getWidthPx(marginHorizontal)),
                        constraints: BoxConstraints(
                          minHeight: ScreenUtil().getWidthPx(250)
                        ),
                        decoration: BoxDecoration(
                          color: backgroundColor != null ? backgroundColor : Theme.of(context).scaffoldBackgroundColor,
                          borderRadius: borderRadius != null ? borderRadius : BorderRadius.all(Radius.circular(ScreenUtil().getWidthPx(30))),
                        ),
                        child: Stack(
                          children: <Widget>[
                            //isShowTitle是否显示title
                            showTitle
                            ? Container(
                              padding: EdgeInsets.only(top: ScreenUtil().getWidthPx(20)),
                              alignment: Alignment.center,
                              child: Cmtext(title ?? "提示", style: TextStyle(fontWeight: FontWeight.w600),),
                            )
                            : Container(),
                            //自定义centerChild, 中间部分, 如果有自定义则显示自定义, 没有则显示subTitle
                            centerChild != null
                            ? Container(
                              margin: EdgeInsets.only(top: showTitle ? ScreenUtil().getWidthPx(74) : 0, bottom: ScreenUtil().getWidthPx(90)),
                              child: centerChild,
                            )
                            : Container(
                              constraints: BoxConstraints(
                                minHeight: ScreenUtil().getWidthPx(86)
                              ),
                              margin: EdgeInsets.only(top: showTitle ? ScreenUtil().getWidthPx(74) : 0, bottom: ScreenUtil().getWidthPx(90)),
                              padding: EdgeInsets.only(left: ScreenUtil().getWidthPx(60), right: ScreenUtil().getWidthPx(60), bottom: ScreenUtil().getWidthPx(8)),
                              alignment: Alignment.center,
                              child: Cmtext(subTitle, style: TextStyle(fontSize: 14, color: Theme.of(context).cardColor, height: 1.2),textAlign: TextAlign.center),
                            ),
                            //参数isBottomBtnWidget是否显示底部buttonWidget
                            isBottomBtnWidget
                            ? Positioned(
                              bottom: ScreenUtil().getWidthPx(90),
                              left: 0,
                              right: 0,
                              child: CommonStyle.bottomLine(),
                            ) : Container(),
                            //参数isBottomBtnWidget是否显示底部buttonWidget
                            isBottomBtnWidget
                            ? Positioned(
                              bottom: 0,
                              left: 0,
                              right: 0,
                              child: Container(
                                width: double.infinity,
                                height: ScreenUtil().getWidthPx(90),
                                child: type == 1
                                ? InkResponse(
                                    highlightColor: Colors.transparent,
                                    radius: 0,
                                    onTap: (){
                                      if(confirmFun != null) {
                                        confirmFun();
                                      } else {
                                        Get.back();
                                      }
                                    },
                                    child: Container(
                                      color: Colors.transparent,
                                      alignment: Alignment.center,
                                      child: Cmtext(confirmText ?? "确定", style: TextStyle(fontWeight: FontWeight.w600),),
                                    ),
                                  )
                                : Row(
                                  children: <Widget>[
                                    Expanded(
                                      child: InkResponse(
                                        highlightColor: Colors.transparent,
                                        radius: 0,
                                        onTap: (){
                                          if(cancelFun != null) {
                                            cancelFun();
                                          } else {
                                            Get.back();
                                          }
                                        },
                                        child: Container(
                                          alignment: Alignment.center,
                                          child: Cmtext(cancelText ?? "取消", style: TextStyle(fontWeight: FontWeight.w600, color: Theme.of(context).cardColor),),
                                        ),
                                      ),
                                    ),
                                    Container(
                                      width: 0.3,
                                      color: Theme.of(context).hintColor,
                                    ),
                                    Expanded(
                                      child: InkResponse(
                                        highlightColor: Colors.transparent,
                                        radius: 0,
                                        onTap: (){
                                          if(confirmFun != null) {
                                            confirmFun();
                                          } else {
                                            Get.back();
                                          }
                                        },
                                        child: Container(
                                          color: Colors.transparent,
                                          alignment: Alignment.center,
                                          child: Cmtext(confirmText ?? "确定", style: TextStyle(fontWeight: FontWeight.w600),),
                                        ),
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                            ) : Container()
                          ],
                        ),
                      )
                    ],
                  ),
                ),
              );
            }
          )
        ),
      ),
      barrierDismissible: false,
      useRootNavigator: false
    );
  }
  // static Future<void> openAddress({@required Function onBack, List selects}) {
  //   List<dynamic> _data = cityData;
  //   if(selects.length == 0) {
  //     selects = [100, 100, 100];
  //   }
  //   int type = 1;
  //   Color _selectColor = Color(0xffFF0000);
  //   Color _defaultColor = Color(0xff666666);
  //   int _provinceIndex = selects[0];
  //   int _cityIndex = selects[1];
  //   int _areaIndex = selects[2];
  //   List<dynamic> _cityList = _provinceIndex != 100 ? _data[_provinceIndex]['city'] : [];
  //   List<dynamic> _areaList = _cityIndex != 100 ? _data[_cityIndex]['city'] : [];


  //   Get.bottomSheet(
  //     // barrierColor: Color.fromRGBO(0, 0, 0, 0.004),
  //     isScrollControlled: true,
  //     useRootNavigator: false,
  //     isDismissible: true,
  //     enableDrag: false,
  //     builder: (context){
  //       return Container(
  //         height: ScreenUtil().getWidthPx(1000),
  //         decoration: BoxDecoration(
  //           color: Color(0xffffffff),
  //           borderRadius: BorderRadius.only(topLeft: Radius.circular(12), topRight: Radius.circular(12))
  //         ),
  //         child: StatefulBuilder(
  //           builder: (BuildContext context2, state) {
  //             return Column(
  //               children: <Widget>[
  //                 Container(
  //                   height: ScreenUtil().getWidthPx(88),
  //                   child: Row(
  //                     mainAxisAlignment: MainAxisAlignment.spaceBetween,
  //                     children: <Widget>[
  //                       Flexible(
  //                         child: InkResponse(
  //                           highlightColor: Colors.transparent,
  //                           radius: 0,
  //                           onTap: (){
  //                             Get.back();
  //                           },
  //                           child: Container(
  //                             width: 54,
  //                             height: 44,
  //                             alignment: Alignment.centerLeft,
  //                             padding: EdgeInsets.only(left: ScreenUtil().getWidthPx(20)),
  //                             child: Icon(IconData(0xe677, fontFamily: 'appIconFont'),color: Colors.black, size: 20),
  //                           ),
  //                         ),
  //                       ),
  //                       Flexible(
  //                         child: Container(
  //                           child: Cmtext('请选择'),
  //                         ),
  //                       ),
  //                       Flexible(
  //                         child: InkResponse(
  //                           highlightColor: Colors.transparent,
  //                           radius: 0,
  //                           onTap: (){
  //                             state(() {
  //                               selects = [_provinceIndex, _cityIndex, _areaIndex];
  //                             });
  //                             String _backData = (_provinceIndex != 100 ? '${_data[_provinceIndex]['name']}' : '') + (_cityIndex != 100 ? '${_cityList[_cityIndex]['name']}' : '') + (_areaIndex != 100 ? '${_areaList[_areaIndex]['name']}' : '');
  //                             onBack(_backData, selects);
  //                             Get.back();
  //                           },
  //                           child: Container(
  //                             width: 54,
  //                             height: 44,
  //                             alignment: Alignment.centerRight,
  //                             padding: EdgeInsets.only(right: ScreenUtil().getWidthPx(20)),
  //                             child: Cmtext('确定', style: TextStyle(fontSize: 14),)
  //                           ),
  //                         ),
  //                       ),
  //                     ],
  //                   ),
  //                 ),
  //                 CommonStyle.bottomLine(),
  //                 Container(
  //                   height: ScreenUtil().getWidthPx(88),
  //                   padding: EdgeInsets.only(left: ScreenUtil().getWidthPx(10)),
  //                   child: Row(
  //                     children: <Widget>[
  //                       InkResponse(
  //                         highlightColor: Colors.transparent,
  //                         radius: 0.0,
  //                         onTap: (){
  //                           print('省');
  //                         },
  //                         child: Container(
  //                           margin: EdgeInsets.only(right: ScreenUtil().getWidthPx(20)),
  //                           child: Cmtext(_provinceIndex != 100 ?'${_data[_provinceIndex]['name']}' : '请选择', style: TextStyle(fontSize: 14, color: Color(0xff888888)),),
  //                         ),
  //                       ),
  //                       InkResponse(
  //                         highlightColor: Colors.transparent,
  //                         radius: 0.0,
  //                         onTap: (){
  //                           print('市');
  //                         },
  //                         child: Container(
  //                           margin: EdgeInsets.only(right: ScreenUtil().getWidthPx(20)),
  //                           child: Cmtext(_cityIndex != 100 ?'${_cityList[_cityIndex]['name']}' : '', style: TextStyle(fontSize: 14, color: Color(0xff888888)),),
  //                         ),
  //                       ),
  //                       InkResponse(
  //                         highlightColor: Colors.transparent,
  //                         radius: 0.0,
  //                         onTap: (){
  //                           print('区');
  //                           state(() {
  //                             type = 3;
  //                           });
  //                         },
  //                         child: Container(
  //                           child: Cmtext(_areaIndex != 100 ?'${_areaList[_areaIndex]['name']}' : '', style: TextStyle(fontSize: 14, color: Color(0xff888888)),),
  //                         ),
  //                       ),
  //                     ],
  //                   ),
  //                 ),
  //                 CommonStyle.bottomLine(),
  //                 Expanded(
  //                   child: Container(
  //                     child: Stack(
  //                       children: <Widget>[
  //                         Positioned(
  //                           top: 0,
  //                           bottom: 0,
  //                           left: 0,
  //                           right: ScreenUtil.getInstance().screenWidth/3*2,
  //                           child: Container(
  //                             child: ScrollConfiguration(
  //                               behavior: MyBehavior(),
  //                               child: ListView.builder(
  //                                 physics: ClampingScrollPhysics(),
  //                                 itemCount: _data.length, 
  //                                 itemBuilder: (BuildContext context, index) {
  //                                   return InkResponse(
  //                                     onTap: (){
  //                                       state(() {
  //                                         _cityList = _data[index]['city'];
  //                                         _areaList = [];
  //                                         _provinceIndex = index;
  //                                         _cityIndex = 100;
  //                                         _areaIndex = 100;
  //                                       });
  //                                     },
  //                                     child: Container(
  //                                       height: ScreenUtil().getWidthPx(88),
  //                                       child: Column(
  //                                         children: <Widget>[
  //                                           Container(
  //                                             height: ScreenUtil().getWidthPx(87),
  //                                             padding: EdgeInsets.only(left: ScreenUtil().getWidthPx(10)),
  //                                             alignment: Alignment.centerLeft,
  //                                             child: Cmtext('${_data[index]['name']}', style: TextStyle(color: _provinceIndex == index ? _selectColor : _defaultColor, fontSize: 14),),
  //                                           ),
  //                                           CommonStyle.bottomLine()
  //                                         ],
  //                                       ),
  //                                     ),
  //                                   );
  //                                 }
  //                               ),
  //                             )
  //                           ),
  //                         ),
  //                         Positioned(
  //                           top: 0,
  //                           bottom: 0,
  //                           left: ScreenUtil.getInstance().screenWidth/3,
  //                           right: ScreenUtil.getInstance().screenWidth/3,
  //                           child: Container(
  //                             child: ListView.builder(
  //                               physics: ClampingScrollPhysics(),
  //                               itemCount: _cityList.length, 
  //                               itemBuilder: (BuildContext context, index) {
  //                                 return InkResponse(
  //                                   onTap: (){
  //                                     state(() {
  //                                       _areaList = _cityList[index]['area'];
  //                                       _cityIndex = index;
  //                                       _areaIndex = 100;
  //                                     });
  //                                   },
  //                                   child: Container(
  //                                     height: ScreenUtil().getWidthPx(88),
  //                                     child: Column(
  //                                       children: <Widget>[
  //                                         Container(
  //                                           height: ScreenUtil().getWidthPx(87),
  //                                           padding: EdgeInsets.only(left: ScreenUtil().getWidthPx(10)),
  //                                           alignment: Alignment.centerLeft,
  //                                           child: Cmtext('${_cityList[index]['name']}', style: TextStyle(color: _cityIndex == index ? _selectColor : _defaultColor, fontSize: 14),),
  //                                         ),
  //                                         CommonStyle.bottomLine()
  //                                       ],
  //                                     ),
  //                                   ),
  //                                 );
  //                               }
  //                             ),
  //                           ),
  //                         ),
  //                         Positioned(
  //                           top: 0,
  //                           bottom: 0,
  //                           left: ScreenUtil.getInstance().screenWidth/3*2,
  //                           right: 0,
  //                           child: Container(
  //                             child: ListView.builder(
  //                               physics: ClampingScrollPhysics(),
  //                               itemCount: _areaList.length, 
  //                               itemBuilder: (BuildContext context, index) {
  //                                 return InkResponse(
  //                                   onTap: (){
  //                                     state(() {
  //                                       _areaIndex = index;
  //                                     });
  //                                   },
  //                                   child: Container(
  //                                     height: ScreenUtil().getWidthPx(88),
  //                                     child: Column(
  //                                       children: <Widget>[
  //                                         Container(
  //                                           height: ScreenUtil().getWidthPx(87),
  //                                           padding: EdgeInsets.only(left: ScreenUtil().getWidthPx(10)),
  //                                           alignment: Alignment.centerLeft,
  //                                           child: Cmtext('${_areaList[index]['name']}', style: TextStyle(color: _areaIndex == index ? _selectColor : _defaultColor, fontSize: 14),),
  //                                         ),
  //                                         CommonStyle.bottomLine()
  //                                       ],
  //                                     ),
  //                                   ),
  //                                 );
  //                               }
  //                             ),
  //                           ),
  //                         ),
  //                       ],
  //                     ),
  //                   ),
  //                 )
  //               ],
  //             );
  //           }
  //         )
  //       );
  //     }
  //   );
  // }

  static void toast(String text){
    BotToast.showText(text: text, textStyle: TextStyle(fontSize: ScreenUtil().getSp(14), color: Colors.white),align: Alignment(0, 0.7));
  }
}


/**
 * 用法:
 * CmUtils.openDialog(
                  subTitle: '提示马嵬坡',
                  cancelFun: cancelFun,
                  confirmFun: confirmFun
                );
 */